package com.joven.shelltest.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author wzj
 * @Date 2023-11-23 14:29
 **/
@Component
@WebFilter(filterName = "HttpServletRequestFilter", urlPatterns = "/*")
public class HttpServletRequestFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 防止流读取一次后就没有了, 所以需要将流继续写出去
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        // 这里将原始request传入，读出流并存储
        ServletRequest requestWrapper = new BodyReaderHttpServletRequestWrapper(httpServletRequest);

//        String requestUrl = ((HttpServletRequest) requestWrapper).getRequestURL().toString();
        // 这里将原始request替换为包装后的request，此后所有进入controller的request均为包装后的
        filterChain.doFilter(requestWrapper, servletResponse);//
    }
}
